<?php
declare (strict_types = 1);

namespace app\admin\model;
use think\facade\Config;
use think\facade\Db;
use think\facade\View;
use think\Model;
use think\model\concern\SoftDelete;

/**
 * @mixin \think\Model
 */
class order extends Model
{
    //
    use SoftDelete;
    protected $deleteTime = "del_time";
    protected $pk = 'order_id';
    //模糊搜索
    protected $autoWriteTimestamp = true;
    protected $createTime = 'add_time';
    protected $updateTime = 'last_update';
    public function searchOrderSnAttr($query,$value,$data){
        if (''!==$value){
            $query->whereLike('order_sn','%'.$value.'%');
        }
    }
    //价格区间
    public function searchOrderAmountsAttr($query,$value,$data){
        if (!empty($value)){
            $query->where('order_amount','>=',$value);
        }
    }

    public function searchOrderAmountssAttr($query,$value,$data){
        if (!empty($value)){
            $query->where('order_amount','<=',$value);
        }
    }

    public function searchOrderAmountAttr($query,$value){
        if (!empty($value) && is_array($value) && 2 === count($value)){
            $query->whereBentween('qrice',$value);
        }
    }
 
    public function searchCidAttr($query,$value){
        if (isset($value) && is_array($value)){
            $query->whereIn('status',$value);
        }
    }
    //分页
    public  function searchPageAttr($query,$value){}
    //时间转换
    public function getAddTimeAttr($value){
        return date('Y-m-d H:i:s',$value);
    }

    public function getPostscriptAttr($value){
        return $value==''?'该用户未填写备注':$value;
    }
    public static function getOrderList($data = [],$query = [],$page = false,$res=false){
        $field=[
            'order_id','order_sn','buyer_id','buyer_name','buyer_email','status','add_time','payment_id','order_amount',
            'add_time','postscript',
            'status_id','order_status',
        ];
        if ($res){
            $asd = self::onlyTrashed()->alias('g');
        }else{
            $asd = self::alias('g');
        }
        $model = $asd->field($field)
            ->join(['ecm_order_status'=>'c'],'c.status_id=g.status','LEFT')
            ->withSearch(array_keys($data),$data);
        $db=Db::table('ecm_order_status')->field(true);


        $list_rows = Config::has('view.list_rows')?Config::get('view.list_rows'):15;

        if ($page){
            $result = $model->select();
        }else{
            $result = $model->paginate([
                'list_rows' => $list_rows,
                'query'     => $query,
            ]);
        }
        $arr=$db->select()->toArray();
        return [$result,$arr];
    }
}
